//
//  RTStatusListModelView.swift
//  Weibo
//
//  Created by Brisinga on 16/3/6.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit
import SDWebImage

class RTStatusListModelView: NSObject {
    
    // status数组
    var statuesViewModelArr: [RTStatusViewModel]?
    
    // Mark-加载网络数据
    func loadData(isPull: Bool,callBack:(isSuccess: Bool)->()) {
        
        
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        var sinceId: Int64 = 0
        var maxId: Int64 = 0
        
        if isPull {
        
            maxId = statuesViewModelArr?.last?.status?.id ?? 0
            
            if maxId != 0 {
                
                maxId--
            }
        }else{
        
            sinceId = statuesViewModelArr?.first?.status?.id ?? 0
        
        }
        
        
        let parameters = [
            
            "access_token": RTUserAcountViewModel.sharedAccountViewModel.access_token!,
            
//            "count": 1
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        
        
        HMNetworkTools.sharedTools.requst(.GET, urlString: urlString, parameters: parameters) { (responseObject, error) -> () in
            
            if  error != nil {
                
                print("数据请求错误")
                
                callBack(isSuccess: false)
                
                return
            }
            
            print(responseObject)
            
            let dicts = responseObject!["statuses"] as! [[String: AnyObject]]
            
            var tempArr = [RTStatusViewModel]()
            
            for dict in dicts {
                
                let statues = RTStatues(dict: dict)
                
                let statusViewModel = RTStatusViewModel(status: statues)
                
                tempArr.append(statusViewModel)
            }
            
            if self.statuesViewModelArr == nil {
            
            self.statuesViewModelArr = [RTStatusViewModel]()
                
            }
            
            
            if isPull {
            
                self.statuesViewModelArr = self.statuesViewModelArr! + tempArr
                
            }else {
            
                self.statuesViewModelArr = tempArr + self.statuesViewModelArr!
            }
            
//            cashSingleImage(tempArr, callBack: callBack)
            
            callBack(isSuccess: true)
        }
    }
    
}


private func cashSingleImage(viewModels: [RTStatusViewModel],callBack:(isSuccess: Bool)->()) {

    // 初始化队列组
    let group = dispatch_group_create()
    let queue = dispatch_get_main_queue()
    
    // 循环
    for viewModel in viewModels {
        
        var urls = viewModel.status?.pic_urls
        
        if urls == nil || urls?.count == 0 {
        
            urls = viewModel.status?.retweeted_status?.pic_urls
        }
        
        
        if urls?.count == 1 {
        
            guard let urlString = urls?.first?.thumbnail_pic else {
                
                continue
            }
            
            // 做入组操作
            dispatch_group_enter(group)
            
            SDWebImageManager.sharedManager().downloadImageWithURL(NSURL(string: urlString), options: [], progress: nil, completed: { (image, error, _, _, url) -> Void in
                
                print("下载完成,地址是\(url.absoluteString))")
                
                
                // 队列离开操作
                dispatch_group_leave(group)
            })
            
        }
        
    }
    // 发送通知
    dispatch_group_notify(group, queue) { () -> Void in
        
        callBack(isSuccess: true)
    }
}

